package com.yuerlive.paperplane.timeline;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.yuerlive.paperplane.R;
import com.yuerlive.paperplane.data.ZhihuDailyNewsQuestion;

import java.util.Calendar;
import java.util.List;
import java.util.TimeZone;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Admin on 2017/10/12.
 */

public class ZhihuDailyFragment extends Fragment implements ZhihuDailyContract.View {
    @BindView(R.id.recychler_view)
    RecyclerView recychlerView;
    @BindView(R.id.empty_view)
    LinearLayout emptyView;
    @BindView(R.id.refresh_layout)
    SwipeRefreshLayout refreshLayout;
    private int mYear, mMonth, mDay;
    private LinearLayoutManager mLayoutManager;
    private ZhihuDailyContract.Presenter mPresenter;
    private FloatingActionButton fab;
    private int mListSize = 0;
    private boolean mIsFirstLoad = true;
    private ZhihuDailyNewsAdapter mAdapter;

    public static ZhihuDailyFragment newInstance() {
        return new ZhihuDailyFragment();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Calendar c = Calendar.getInstance();
        c.setTimeZone(TimeZone.getTimeZone("GMT+08"));
        mYear = c.get(Calendar.YEAR);
        mMonth = c.get(Calendar.MONTH);
        mDay = c.get(Calendar.DAY_OF_MONTH);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_timeline_page, container, false);
        ButterKnife.bind(this,view);
        fab = (FloatingActionButton) getActivity().findViewById(R.id.fab);
        refreshLayout.setColorSchemeColors(ContextCompat.getColor(getContext(), R.color.colorAccent));
        mLayoutManager=new LinearLayoutManager(getContext());
        recychlerView.setLayoutManager(mLayoutManager);
        refreshLayout.setOnRefreshListener(()->{
            Calendar c =Calendar.getInstance();
            c.setTimeZone(TimeZone.getTimeZone("GMT+08"));
            mPresenter.loadNews(true,true,c.getTimeInMillis());
        });
        recychlerView.setOnScrollListener(new RecyclerView.OnScrollListener(){
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if(dy>0){
                 fab.hide();
                    if(mLayoutManager.findLastCompletelyVisibleItemPosition()==mListSize-1){
                        loadMore();
                    }
                }else{
                    fab.show();
                }

            }
        });
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        mPresenter.start();
        Calendar c = Calendar.getInstance();
        c.setTimeZone(TimeZone.getTimeZone("GMT+08"));
        c.set(mYear, mMonth, mDay);
        setLoadingIndicator(mIsFirstLoad);
        if(mIsFirstLoad){
            mPresenter.loadNews(true,false,c.getTimeInMillis());
            mIsFirstLoad=false;
        }else{
            mPresenter.loadNews(false,false,c.getTimeInMillis());
        }
    }

    private void loadMore(){
        Calendar c = Calendar.getInstance();
        c.set(mYear, mMonth, --mDay);
        mPresenter.loadNews(true, false, c.getTimeInMillis());
    }
    @Override
    public void setPresenter(ZhihuDailyContract.Presenter presenter) {
     mPresenter=presenter;
    }

    @Override
    public void initViews(View view) {

    }

    @Override
    public boolean isActive() {
        return isAdded() && isResumed();
    }

    @Override
    public void setLoadingIndicator(boolean active) {

    }

    @Override
    public void showResult(@NonNull List<ZhihuDailyNewsQuestion> list) {
      if(mAdapter ==null){
          mAdapter=new ZhihuDailyNewsAdapter(list,getContext());
          recychlerView.setAdapter(mAdapter);
      }else{
          mAdapter.updataData(list);
      }
      mListSize =list.size();
        emptyView.setVisibility(list.isEmpty()?View.VISIBLE:View.INVISIBLE);

    }

}
